﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.EventService.SuperSocket.Core.Client;
using A2DFramework.EventService.SuperSocket.Core;
using A2DFramework.EventService.Configuration;
using A2DFramework.InstanceService;

namespace A2DFramework.EventService.Factory
{
    public static class A2DSocketFactory
    {
        /// <summary>
        /// 获得一个A2DTcpSocket对象（不用考虑是否连接到了服务器，会自动建立连接、retry connect）
        /// </summary>
        /// <returns></returns>
        public static A2DTcpBroker GetA2DSocketClient()
        {
            string serverIp=Configuration.EventServiceConfiguration.SuperSocketSettings.Address;
            int serverPort=Configuration.EventServiceConfiguration.SuperSocketSettings.Port;
            double retryInterval=Configuration.EventServiceConfiguration.SuperSocketSettings.RetryConnectInterval;


            var funcPerConnected = new Action<A2DTcpBroker>((client)=>{
                client.Send(Helper.GenerateDeclareCommand(Configuration.EventServiceConfiguration.SuperSocketSettings.Identifier));
                client.AttachEvent(Starter.eventResolver.Resolve);

                Starter.RegisterEvents();
        });

            return A2DTcpClientManager.GetClientSocket(serverIp, serverPort, retryInterval, funcPerConnected);
        }
    }
}